﻿<#
    Script  : ImportPSModule
    Author  : Riwut Libinuko
    Blog    : http://blog.libinuko.com 
    Copyright :© IdeasFree - cakriwut@gmail.com, 2013. All rights reserved.
#>

function Import-PSModule-Install
{
  param(
      [Parameter(Mandatory=$true,Position=0,ParameterSetName="Install")]
      [string] $packageName,      
      [Parameter(Mandatory=$false,Position=1,ParameterSetName="Install")]
      [string] $source= $globalPkgSource,
	  [string] $installDir = $globalPkgInstallationDir,
	  [Parameter(ParameterSetName="Install")]
      [string] $version =$null,
	  [Parameter(ParameterSetName="Install")]
      [bool] $latest= $true,
	  [Parameter(ParameterSetName="Install")]
      [string] $tags='PSModule',
	  [Parameter(ParameterSetName="Install")]
      [bool] $install = $true
   )
  
  if((test-path $source))
   {
      gci $source | remove-item -recurse -force
   }
   #Get feed url
   $serviceBase = GetPackageUrl $source
   $feedUrl = $serviceBase + "Packages"
     
   
   if((GetPackage -PackageName $packageName -Source $source -distFolder $globalPkgDistDir -Latest $latest -Version $version -ExtraFilter " substringof(`'$tags`',Tags)"))
   {   
     #call InstallPkg , from local distribution to avoid proxy auth problem     
     if(($pkgLocation = InstallPkg -packageName $packageName -nodeps $false -Source $globalPkgDistDir -installDir $installDir))
     {        
       #in the installation folder, find PSModuleInstall.ps1 / PSModuleUninstall.ps1
       if($install)
       {         
         if(($ps1 = gci $pkgLocation -recurse |? { $_.name -match "PSModuleInstall\.ps1" } | sort name -Descending | select -First 1))
         {
            Invoke-Expression "$($ps1.FullName)"            
         } 
         
         write-host "Trying to automatically load all module files."
         $pkgLocation |% {
            Register-PSModule -ModuleDirectory $_
         }                  
         Write-host ""
         Write-host "$packageName install operation has been completed."
         
       } else {
         if(($ps1 = gci $pkgLocation -recurse |? { $_.name -match "PSModuleUnInstall\.ps1" } | sort name -Descending | select -First 1))
         {
            Invoke-Espression "$($ps1.FullName)"            
         } 
         
         Unregister-PSModule -ModuleDirectory $pkgLocation -removeAll $true         
         split-path $pkgLocation | gci |? { $_ -match "$packageName(\.\d)*`$" } |% {
             Write-Host "..Removing installation history $($_.FullName)"
             remove-item $_.FullName -recurse -force #| out-null 
        }
         Write-Host "$packageName has been removed."
         Write-Host "All dependencies item are not removed. You may remove it manually if no other component are using it."
       }
       
	  } else{
        write-host "Can not find $packageName in the $source. Ensures both package and dependencies are exists." -foregroundcolor red
     }
   }
}